﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-08-20 17:28:21Z</CreationDate>
</File>
*/
#endregion

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Outcoder.Data
{
	/* TODO: [DV] Comment. */
	public class Serializer
	{
		public string Serialize<T>(T data)
		{
			using (var memoryStream = new MemoryStream())
			{
				var serializer = new DataContractJsonSerializer(typeof(T));
				serializer.WriteObject(memoryStream, data);

				memoryStream.Seek(0, SeekOrigin.Begin);

				var reader = new StreamReader(memoryStream);
				string content = reader.ReadToEnd();
				return content;
			}
		}

		public T Deserialize<T>(string json)
		{
			using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
			{
				var serializer = new DataContractJsonSerializer(typeof(T));
				T theObject = (T)serializer.ReadObject(stream);
				return theObject;
			}
		}
	}
}
